﻿using Nop.Core;
using Nop.Core.Caching;
using Nop.Core.Data;
using Nop.Core.Domain.WindAlert;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Nop.Services.WindAlert
{
    /// <summary>
    /// Camera service
    /// </summary>
    public partial class CameraService : ICameraService
    {
        #region Constants
        private const string CAMERAS_ALL_KEY = "Nop.camera.all-{0}";
        private const string CAMERAS_BY_ID_KEY = "Nop.camera.id-{0}";
        private const string CAMERAS_PATTERN_KEY = "Nop.camera.";
        #endregion

        #region Fields

        private readonly IRepository<Camera> _cameraRepository;
        private readonly ICacheManager _cacheManager;
        #endregion

        #region Ctor

        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="cacheManager">Cache manager</param>
        /// <param name="cameraRepository">Live Camera repository</param>
        public CameraService(ICacheManager cacheManager,
            IRepository<Camera> cameraRepository)
        {
            this._cacheManager = cacheManager;
            this._cameraRepository = cameraRepository;
        }
        #endregion

        #region Methods

        /// <summary>
        /// Deletes a camera
        /// </summary>
        /// <param name="camera">Camera</param>
        public virtual void DeleteCamera(Camera camera)
        {
            if (camera == null)
                throw new ArgumentNullException("camera");

            camera.Deleted = true;
            UpdateCamera(camera);
        }

        /// <summary>
        /// Gets all cameras
        /// </summary>
        /// <param name="showHidden">A value indicating whether to show hidden records</param>
        /// <param name="cameraId">Camera Id</param>
        /// <returns>Camera collection</returns>
        public virtual IList<Camera> GetAllCameras(bool showHidden = false)
        {
            string key = string.Format(CAMERAS_ALL_KEY, showHidden);
            return _cacheManager.Get(key, () =>
            {
                var query = from m in _cameraRepository.Table
                            orderby m.DisplayOrder
                            where (showHidden || m.Published) &&
                            !m.Deleted
                            select m;
                var cameras = query.ToList();
                return cameras;
            });
        }

        /// <summary>
        /// Gets all cameras
        /// </summary>
        /// <param name="pageIndex">Page index</param>
        /// <param name="pageSize">Page size</param>
        /// <param name="showHidden">A value indicating whether to show hidden records</param>
        /// <param name="cameraId">Camera Id</param>
        /// <returns>Cameras</returns>
        public virtual IPagedList<Camera> GetAllCameras(int pageIndex, int pageSize, bool showHidden = false)
        {
            var cameras = GetAllCameras(showHidden);
            return new PagedList<Camera>(cameras, pageIndex, pageSize);
        }

        /// <summary>
        /// Gets a camera
        /// </summary>
        /// <param name="cameraId">Camera identifier</param>
        /// <returns>Camera</returns>
        public virtual Camera GetCameraById(int cameraId)
        {
            if (cameraId == 0)
                return null;

            string key = string.Format(CAMERAS_BY_ID_KEY, cameraId);
            return _cacheManager.Get(key, () =>
            {
                var camera = _cameraRepository.GetById(cameraId);
                return camera;
            });
        }

        /// <summary>
        /// Inserts a camera
        /// </summary>
        /// <param name="camera">Camera</param>
        public virtual void InsertCamera(Camera camera)
        {
            if (camera == null)
                throw new ArgumentNullException("camera");

            _cameraRepository.Insert(camera);

            //cache
            _cacheManager.RemoveByPattern(CAMERAS_PATTERN_KEY);
        }

        /// <summary>
        /// Updates the camera
        /// </summary>
        /// <param name="camera">Camera</param>
        public virtual void UpdateCamera(Camera camera)
        {
            if (camera == null)
                throw new ArgumentNullException("camera");

            _cameraRepository.Update(camera);

            //cache
            _cacheManager.RemoveByPattern(CAMERAS_PATTERN_KEY);
        }



        #endregion
    }
}
